package com.gengyun.gengyunacquistion.config;

import com.gengyun.gengyunacquistion.scheduled.DataCollectHistory;
import com.gengyun.gengyunacquistion.scheduled.DataAcquisition;
import com.gengyun.gengyunacquistion.scheduled.DataAcquisitionDaily;
import com.gengyun.gengyunacquistion.scheduled.DataUavMultispectralImage;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {


    /**
     * 创建 job 对象
     * @return
     */
    @Bean(name="jobDetailFactoryBean")
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        // 关联自己的job类
        factoryBean.setJobClass(DataAcquisition.class);
        factoryBean.setName("job1");
        return factoryBean;
    }

    @Bean(name="jobDetailFactoryDailyBean" )
    public JobDetailFactoryBean jobDetailFactoryDailyBean(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        // 关联自己的job类
        factoryBean.setJobClass(DataAcquisitionDaily.class);
        factoryBean.setName("job2");
        return factoryBean;
    }

    @Bean(name="CollectHistoryBean" )
    public JobDetailFactoryBean CollectHistoryBean(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        // 关联自己的job类
        factoryBean.setJobClass(DataCollectHistory.class);
        factoryBean.setName("job3");
        return factoryBean;
    }

    @Bean(name="UavMultispectralImageBean" )
    public JobDetailFactoryBean UavMultispectralImageBean(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        // 关联自己的job类
        factoryBean.setJobClass(DataUavMultispectralImage.class);
        factoryBean.setName("job4");
        return factoryBean;
    }


    /**
     * 创建 Trigger 对象
     * @return
     */
    @Bean(name="cronTriggerFactoryBean" )
    public CronTriggerFactoryBean cronTriggerFactoryBean(@Qualifier("jobDetailFactoryBean")JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        // 关联jobDetail对象
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        // 设置时间
        cronTriggerFactoryBean.setCronExpression("1/5 * * * * ?");
        cronTriggerFactoryBean.setName("trigger1");
        return cronTriggerFactoryBean;
    }

    @Bean(name="cronTriggerFactoryDailyBean" )
    public CronTriggerFactoryBean cronTriggerFactoryDailyBean(@Qualifier("jobDetailFactoryDailyBean")JobDetailFactoryBean jobDetailFactoryDailyBean) {
        CronTriggerFactoryBean cronTriggerFactoryDailyBean = new CronTriggerFactoryBean();
        // 关联jobDetail对象
        cronTriggerFactoryDailyBean.setJobDetail(jobDetailFactoryDailyBean.getObject());
        // 设置时间
        cronTriggerFactoryDailyBean.setCronExpression("1/5 * * * * ?");  //0 0 6 * * ?
        cronTriggerFactoryDailyBean.setName("trigger2");
        return cronTriggerFactoryDailyBean;
    }

    @Bean(name="cronTriggerCollectHistory" )
    public CronTriggerFactoryBean cronTriggerCollectHistory(@Qualifier("CollectHistoryBean")JobDetailFactoryBean CollectHistoryBean) {
        CronTriggerFactoryBean cronTriggerCollectHistory = new CronTriggerFactoryBean();
        // 关联jobDetail对象
        cronTriggerCollectHistory.setJobDetail(CollectHistoryBean.getObject());
        // 设置时间
        cronTriggerCollectHistory.setCronExpression("1/5 * * * * ?");  //0 0 6 * * ?
        cronTriggerCollectHistory.setName("trigger3");
        return cronTriggerCollectHistory;
    }

    @Bean(name="cronTriggerUavMultispectralImage" )
    public CronTriggerFactoryBean cronTriggerUavMultispectralImage(@Qualifier("UavMultispectralImageBean")JobDetailFactoryBean UavMultispectralImageBean) {
        CronTriggerFactoryBean cronTriggerUavMultispectralImage = new CronTriggerFactoryBean();
        // 关联jobDetail对象
        cronTriggerUavMultispectralImage.setJobDetail(UavMultispectralImageBean.getObject());
        // 设置时间
        cronTriggerUavMultispectralImage.setCronExpression("1/5 * * * * ?");  //0 0 6 * * ?
        cronTriggerUavMultispectralImage.setName("trigger4");
        return cronTriggerUavMultispectralImage;
    }












    /**
     * 创建 Schedule 对象
     * @return
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("cronTriggerFactoryBean")CronTriggerFactoryBean cronTriggerFactoryBean,@Qualifier("cronTriggerFactoryDailyBean")CronTriggerFactoryBean cronTriggerFactoryDailyBean
            ,@Qualifier("cronTriggerCollectHistory")CronTriggerFactoryBean cronTriggerCollectHistory,@Qualifier("cronTriggerUavMultispectralImage")CronTriggerFactoryBean cronTriggerUavMultispectralImage, MyAdaptableJobFactory myAdaptableJobFactory) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        // 关联Trigger对象
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject(),cronTriggerFactoryDailyBean.getObject(),cronTriggerCollectHistory.getObject(),cronTriggerUavMultispectralImage.getObject());

        // 配置注入
        schedulerFactoryBean.setJobFactory(myAdaptableJobFactory);
        return schedulerFactoryBean;
    }
}
